BoxPlot mtcars

Dr.Cruz

2022-06-24

Boxplot

  1. Entendiendo Box Plot y Factor Type value Archivo
  2. Aplicando Box PLot a la Base Clima Archivo2

Motor Trend Car Road Tests

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

mpg Miles/(US) gallon disp Displacement (cu.in.)
cyl Number of cylinders vs Engine (0 = V-shaped, 1 = straight)
hp Gross horsepower am Transmission (0 = automatic, 1 = manual)
drat Rear axle ratio gear Number of forward gears
wt Weight (1000 lbs) carb Number of carburetors
qsec 1/4 mile time

Librerias

library(DataExplorer)
library(ggplot2)
library(plotly)

Datos

data("mtcars")
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Boxplot global

#create_report(mtcars)
boxplot(mtcars$mpg)

Analisis de variables en funcion del numero de Cilindors = cyl

plot_boxplot(mtcars, by="cyl")

Análisis del número de cilindros vs mpg

p <- ggplot(mtcars, aes(as.factor(cyl),mpg)) +
     geom_boxplot() + 
     geom_jitter()
p

p1 <- p + theme(panel.background = element_rect(fill = "#72efdd"))
p1

ggplotly(p1)

Analisis de numero de cilindros vs mpg con color

p <- ggplot(mtcars, aes(as.factor(cyl),mpg)) +
    geom_boxplot(fill="blue",color="red", outlier.shape = 23,outlier.size = 2)
p

Analisis de numero de cilindros vs peso wt

p <- ggplot(mtcars, aes(as.factor(cyl),wt)) +
  geom_boxplot() + 
  geom_jitter()
p

Analisis de numero del peso del auto vs cyl = 8

data3 <- subset(mtcars, cyl==8)
ggplot(data3, aes(x=wt)) + geom_density(fill="slateblue")

ggplot(data3, aes(x=wt)) + geom_boxplot(fill="blue")

p <- ggplot(mtcars, aes(as.factor(cyl),hp)) +
  geom_boxplot() + 
  geom_jitter()
p

p <- ggplot(mtcars, aes(as.factor(cyl),hp)) +
  geom_boxplot() + 
  geom_jitter()
p

plot_boxplot(mtcars, by="am")

p <- ggplot(mtcars, aes(as.factor(am),hp)) +
  geom_boxplot() + 
  geom_jitter()
p

p <- ggplot(mtcars, aes(as.factor(am),mpg)) +
  geom_boxplot() + 
  geom_jitter()
p

coplot(mpg ~ hp | as.factor(cyl), data = mtcars,
       panel = panel.smooth, rows = 1)

coplot(qsec ~ hp | as.factor(cyl), data = mtcars,
       panel = panel.smooth, rows = 1)

# for summary() or bivariate plots:
mtcars2 <- within(mtcars, {
   vs <- factor(vs, labels = c("V", "S"))
   am <- factor(am, labels = c("automatic", "manual"))
   cyl  <- ordered(cyl)
   gear <- ordered(gear)
   carb <- ordered(carb)})
summary(mtcars2)
##       mpg        cyl         disp             hp             drat      
##  Min.   :10.40   4:11   Min.   : 71.1   Min.   : 52.0   Min.   :2.760  
##  1st Qu.:15.43   6: 7   1st Qu.:120.8   1st Qu.: 96.5   1st Qu.:3.080  
##  Median :19.20   8:14   Median :196.3   Median :123.0   Median :3.695  
##  Mean   :20.09          Mean   :230.7   Mean   :146.7   Mean   :3.597  
##  3rd Qu.:22.80          3rd Qu.:326.0   3rd Qu.:180.0   3rd Qu.:3.920  
##  Max.   :33.90          Max.   :472.0   Max.   :335.0   Max.   :4.930  
##        wt             qsec       vs             am     gear   carb  
##  Min.   :1.513   Min.   :14.50   V:18   automatic:19   3:15   1: 7  
##  1st Qu.:2.581   1st Qu.:16.89   S:14   manual   :13   4:12   2:10  
##  Median :3.325   Median :17.71                         5: 5   3: 3  
##  Mean   :3.217   Mean   :17.85                                4:10  
##  3rd Qu.:3.610   3rd Qu.:18.90                                6: 1  
##  Max.   :5.424   Max.   :22.90                                8: 1
p <- ggplot(mtcars2, aes(gear,mpg)) +
  geom_boxplot() + 
  geom_jitter()
p

ggplotly(p)
library(ggcorrplot)
corr <- round(cor(mtcars), 1)

# Plot
ggcorrplot(corr, hc.order = TRUE, 
           type = "lower", 
           lab = TRUE, 
           lab_size = 3, 
           method="circle", 
           colors = c("tomato2", "white", "springgreen3"), 
           title="Matriz de Correlacion para mtcars", 
           ggtheme=theme_bw)

# Data: numeric variables of the native mtcars dataset
data <- mtcars[ , c(1,3:7)]
 
# Plot
plot(data , pch=20 , cex=1.5 , col="#69b3a2")

library(ggthemes)
g <- ggplot(mtcars, aes(cyl, mpg))
g + geom_boxplot(aes(fill=factor(cyl))) + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6))